<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Title</title>
  <script src="../js/vue-3.2.26.js"></script>
</head>
<body>
<div id="app">
  <h1>{{msg}}</h1>
  <!-- 给组件一个ref值，相当于id，使父组件可以找到子组件 -->
  <hello ref="hello"></hello>
  <button @click="getHello">获取子组件</button>
</div>
</body>
<template id="hello">
  <h2>{{hi}}</h2>
</template>
<script>
  const hello = {
    template: '#hello',
    data() {
      return {
        hi: "hi!"
      }
    },
    methods: {
      getHi() {
        return 'getHi:' + this.hi;
      }
    }
  }
  const App = {
    data() {
      return {
        msg: "msg"
      }
    },
    methods: {
      getHello() {
        // 可以用this.$refs.ref值，获取对应组件
        console.log(this.$refs.hello);
        // 获取子组件的数据
        console.log(this.$refs.hello.hi);
        // 调用子组件的方法
        console.log(this.$refs.hello.getHi());
      }
    },
    components: {
      hello
    }
  }
  const app = Vue.createApp(App).mount("#app");
</script>
</html>
